package com.finance.report.service;


import com.finance.base.constant.BaseConstants;
import com.finance.framework.lang.Page;
import com.finance.framework.lang.PageResultBO;
import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsComittedInfos;
import com.finance.report.bean.pojo.LogisticsCommitted;
import com.finance.report.bean.pojo.LogisticsExtend;
import com.finance.report.dao.LogisticsExtendMapper;
import com.finance.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LogisticsExtendService implements ILogisticsExtendService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private LogisticsExtendMapper logisticsExtendMapper;

	@Override
	public LogisticsExtend getById(Date signTime,Long  id) {
		String tableName = this.getTableName(signTime);
		return logisticsExtendMapper.getById(tableName,id);
	}
	@Cacheable(value = "logisticsExtend",cacheResolver = "signDateCacheResolver",key = "#root.target.getTableName(#signTime)+'_'+#wayBillNum")
	@Override
	public LogisticsExtend getByWayBillNum(Date signTime,String wayBillNum) {
		String tableName = this.getTableName(signTime);
		return this.logisticsExtendMapper.getByWayBillNum(tableName,wayBillNum);
	}
	@Deprecated
	@Override
	public List<LogisticsExtend> listAll(Date signTime){
		String tableName = this.getTableName(signTime);
		return logisticsExtendMapper.listAll(tableName);
	}

	@Override
	public PageResultBO<LogisticsExtend> listByPage(Date signTime,Page page) {
		String tableName = this.getTableName(signTime);
		PageResultBO<LogisticsExtend> resultBO = new PageResultBO<LogisticsExtend>();
		Map param = (Map) page.getParams();
		param.put("tableName", tableName);
		List<LogisticsExtend> list = this.logisticsExtendMapper.listByPage(page);
		resultBO.setDataList(list);
		resultBO.setPageSize(page.getPageSize());
		resultBO.setPageNo(page.getPageNo());
		resultBO.setTotalRecord(page.getTotalRecord());
		return resultBO;
	}

	@Override
	public PageResultBO<LogisticsExtend> extendsInfos(Page page) {
		PageResultBO <LogisticsExtend> pageResultBO = new PageResultBO<LogisticsExtend>();
		List<LogisticsExtend> extendList = logisticsExtendMapper.extendsInfos(page);
		pageResultBO.setTotalRecord(page.getTotalRecord());
		pageResultBO.setPageNo(page.getPageNo());
		pageResultBO.setPageSize(page.getPageSize());
		pageResultBO.setDataList(extendList);
		return pageResultBO;
	}

	@Override
	@CacheEvict(value = "logisticsExtend",cacheResolver = "logisticsDataCacheResolver", key = "#root.target.getTableName(#signTime)+'_'+#logisticsExtend.waybillNum")
	public boolean add(Date signTime, LogisticsExtend logisticsExtend) {
		if(this.getByWayBillNum(signTime, logisticsExtend.getWaybillNum()) == null){
			String tableName = this.getTableName(signTime);
			this.logger.info("运单拓展表新增数据：{}", logisticsExtend.toString());
			return logisticsExtendMapper.add(tableName, logisticsExtend) == 1;
		}else{
			this.logger.info("已存在的运单拓展信息，过滤处理，运单号：{}", logisticsExtend.getWaybillNum());
			return false;
		}
	}

	@Override
 	@Transactional
	public boolean delete(Date signTime,Long id) {
		String tableName = this.getTableName(signTime);
		return logisticsExtendMapper.delete(tableName,id) == 1;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CachePut(value = "logistics",cacheResolver = "logisticsDataCacheResolver", key = "#root.target.getTableName(#signTime)+'_'+#logisticsExtend.waybillNum",condition="#result != null")
	public LogisticsExtend update(Date signTime, LogisticsExtend logisticsExtend) throws Exception {
		String tableName = this.getTableName(signTime);
		boolean result = logisticsExtendMapper.update(tableName,logisticsExtend) == 1;
		this.logger.info("[计算后结果更新]运单拓展表更新数据{} 结果：{}", logisticsExtend.toString(), String.valueOf(result));
		if(!result){
			this.logger.error("运单拓展表更新数据{}失败", logisticsExtend.getWaybillNum());
			throw new Exception(logisticsExtend.getWaybillNum() + "运单拓展表更新数据失败");
		}else{
			return logisticsExtend;
		}
	}

	@Override
	@CachePut(value = "logistics",cacheResolver = "logisticsDataCacheResolver", key = "#root.target.getTableName(#signTime)+'_'+#logisticsExtend.waybillNum",condition="#result != null")
	public LogisticsExtend updateWithdataValid(Date signTime, LogisticsExtend logisticsExtend, String dataValid) throws Exception {
		String tableName = this.getTableName(signTime);
		int from = Integer.parseInt(dataValid);
		int to = Integer.parseInt(logisticsExtend.getDataValid());
		if(from == Privilege.ALL_PRIVILEGE){
			this.logger.info("运单拓展信息无序解析 已完成:{}",logisticsExtend.getWaybillNum());
		}
		boolean result = false;
		if(from >= to){
			this.logger.info("运单拓展表{}跳过状态更新 from {} to {}", new String[]{logisticsExtend.getWaybillNum(),from+"", to+""});
 			logisticsExtend.setDataValid(dataValid + "");
			result = logisticsExtendMapper.update(tableName, logisticsExtend)==1;
		}else{
 			result = logisticsExtendMapper.updateWithdataValid(tableName, logisticsExtend, dataValid) == 1;
		}
		if(!result){
			this.logger.error("运单拓展表更新数据{}失败", logisticsExtend.getWaybillNum());
			throw new Exception(logisticsExtend.getWaybillNum() + "运单拓展表更新数据失败");
		}else{
			return logisticsExtend;
		}

	}

	@Override
	public List<LogisticsCommitted> getManLogistics(String extendTableName, String postmanNo) {
		return logisticsExtendMapper.getManLogistics(extendTableName,postmanNo);
	}

	@Override
	public LogisticsComittedInfos  getLogisticsInfo(String tableName,String waybillNum) {
		return logisticsExtendMapper.getLogisticsInfo(tableName,waybillNum);
	}

	@Override
	public List<String> getPostmans(String extendTableName) {
		return logisticsExtendMapper.getPostmans(extendTableName);
	}

	@Override
	public boolean isFinished(String extendTableName) {
		return logisticsExtendMapper.isFinished(extendTableName)>0;
	}

	/**
	 * 获取操作表名
	 * @return
	 */
	public String getTableName(Date signTime){
		return BaseConstants.SYSCONFIG_KEY_LOGISTICS_EXTEND_TABLE + DateUtil.dateFormat(signTime,"yyMM");
	}
}
